package com.googlecode.lucenedb.lucene;

import java.io.File;
import java.util.Collection;
import java.util.Random;

import junit.framework.TestCase;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.CachingCollector;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MultiCollector;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.grouping.TermAllGroupsCollector;
import org.apache.lucene.search.grouping.TermFirstPassGroupingCollector;
import org.apache.lucene.search.grouping.TermSecondPassGroupingCollector;
import org.apache.lucene.search.grouping.TopGroups;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class Lucene extends TestCase{
	
	public    void   setUp(){
		try{
			StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_33);
			Directory directory =  FSDirectory.open(new File("d:/lucenedbindex/test"));
	        IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_33, analyzer);
	        conf.setOpenMode(OpenMode.CREATE);
	        IndexWriter fsWriter = new IndexWriter(directory, conf);
	        
	        Random ageRandom = new Random();
	        
	        Document doc1 = null;
	        for(int i=0;i<1000;i++){
	        	System.out.println(i);
	        	doc1 = new Document();
	        	doc1.add(new Field("name", randomString(10), Field.Store.YES, Field.Index.ANALYZED));
		        doc1.add(new Field("age", String.valueOf(ageRandom.nextInt(100)), Field.Store.YES, Field.Index.NOT_ANALYZED));
		        doc1.add(new Field("sex", String.valueOf(ageRandom.nextInt(2)), Field.Store.YES, Field.Index.NOT_ANALYZED));
		        fsWriter.addDocument(doc1);
		        if(i%100==0)
		           fsWriter.commit();
		        	
	        }
	        
           /*Document doc2 = new Document();
	        doc2.add(new Field("name", "xiuyan pan", Field.Store.YES, Field.Index.ANALYZED));
	        doc2.add(new Field("age", "21", Field.Store.YES, Field.Index.NOT_ANALYZED));
	        doc2.add(new Field("sex", "1", Field.Store.YES, Field.Index.NOT_ANALYZED));
	        fsWriter.addDocument(doc2);
	        
	        
            Document doc3 = new Document();
	        doc3.add(new Field("name", "xiuyan pan", Field.Store.YES, Field.Index.ANALYZED));
            doc3.add(new Field("age", "20", Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc3.add(new Field("sex", "2", Field.Store.YES, Field.Index.NOT_ANALYZED));
	        fsWriter.addDocument(doc3);
	        
	        
	        Document doc4 = new Document();
	        doc4.add(new Field("name", "pan fde", Field.Store.YES, Field.Index.ANALYZED));
	        doc4.add(new Field("age", "100", Field.Store.YES, Field.Index.NOT_ANALYZED));
	        doc4.add(new Field("sex", "2", Field.Store.YES, Field.Index.NOT_ANALYZED));
	        fsWriter.addDocument(doc4);
	        
	        Document doc5 = new Document();
	        doc5.add(new Field("name", "pan fdedd", Field.Store.YES, Field.Index.ANALYZED));
	        doc5.add(new Field("age", "20", Field.Store.YES, Field.Index.NOT_ANALYZED));
	        doc5.add(new Field("sex", "2", Field.Store.YES, Field.Index.NOT_ANALYZED));
	        fsWriter.addDocument(doc5);*/
	        
	        fsWriter.optimize();
	        
	        fsWriter.close();
	        
	        System.out.println("create index success");
	    	
		}catch(Exception e){
			e.printStackTrace();
		}
   }
	
	
	public void testQuery() throws Exception{
		StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_33);
		Directory directory =  FSDirectory.open(new File("d:/lucenedbindex/test")); 
		IndexSearcher is = new IndexSearcher(directory);  
		QueryParser parser = new QueryParser(Version.LUCENE_33,"name", analyzer); 
		Query query = parser.parse("pan");
		TopDocs hits = is.search(query, 100);
		ScoreDoc[] aa  = hits.scoreDocs;
		
		for (int i = 0; i < aa.length; i++)  
		{  
		    Document doc = is.doc(aa[i].doc);
		    System.out.println(doc.get("name") + "    "+doc.get("age"));  
		}
		
		
	}
	
	private static Random randGen = null;
	private static char[] numbersAndLetters = null;
	private static Object initLock = new Object();
	
	public static final String randomString(int length) {

        if (length < 1) {
            return null;
        }
        if (randGen == null) {
            synchronized (initLock) {
                if (randGen == null) {
                    randGen = new Random();
                    numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" +
                    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
                    //numbersAndLetters = ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
                }
            }
        }
        char [] randBuffer = new char[length];
        for (int i=0; i<randBuffer.length; i++) {
            randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
         //randBuffer[i] = numbersAndLetters[randGen.nextInt(35)];
        }
        return new String(randBuffer);
}
	

	
	public void testStrCompareTo(){
		System.out.println(randomString(10));
	}

}
